package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import com.ly.mpayment.persist.entity.Operators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.service.operators.IOperatorsService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;
@Component
@Path("system/operator/")
public class OperatorRest extends AbsRest{
	@Autowired
	IUserService userService;

	@Autowired
	IOperatorsService operatorService;

	@POST
	@Path("findPage")
	public String findPage(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,  "operatorname","start","pageSize");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> paramets = MapUtil.getMap();
			if(!StringUtils.isEmpty(json.getString("operatorname"))){
				paramets.put(":operatorname", json.getString("operatorname"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.operatorService.findPageOperator(paramets, json.getInt("start"),  json.getInt("pageSize")),
					new String[]{""}));
		}
		return result;
	}

	@POST
	@Path("addOperator")
	public String addOperator(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,  "operatorname","code","remark","status");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Operators operators = new Operators();
			operators.setCode(json.getString("code"));
			operators.setOperatorname(json.getString("operatorname"));
			operators.setRemark(json.getString("status"));
			operators.setStatus(json.getString("status"));
			return this.operatorService.addOperators(operators) ? ResultMessage.getSucceed() : ResultMessage.getFailt("");
		}
		return result;
	}
	@POST
	@Path("updateOperator")
	public String updateOperator(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,  "operatorname","start","pageSize");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return operatorService.updateOperator(json) ? ResultMessage.getSucceed() : ResultMessage.getFailt("");
		}
		return result;
	}
	@POST
	@Path("updateStatusOperator")
	public String updateStatusOperator(String data ,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,  "operatoruid","status");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return operatorService.updateStatusOperator(json) ? ResultMessage.getSucceed() : ResultMessage.getFailt("");
		}
		return result;
	}
}
